// router.js:这个文件专门用于创建整个应用的路由器

// 引入VueRouter
import VueRouter from 'vue-router'

// 引入组件 
import Home from '../views/home/index.vue'
import Sort from '../views/sort/index.vue'
import Shopp from '../views/shopp/index.vue'
import My from '../views/my/index.vue'
import Details from '../components/details.vue'
import Register from '../views/register/index.vue'
import User from '../views/user/index.vue'
 

 
// 创建一个 路由器
const router = new VueRouter({
	routes:[
		{
			path:'/',
			name:'Home',
			alias:'/home',
			component:Home
		},
		{
			path:'/sort',
			name:'Sory',
			component:Sort
		},
		{
			path:'/shopp/:id',
			name:'Shopp',
			component:Shopp
		},
		{
			path:'/my',
			name:'My',
			component:My
		},
		{
			path:'/details/:id',
			name:'Details',
			component:Details
		},
		{
			path:'/register',
			name:'Register',
			component:Register
		},
		{
			path:'/user',
			name:'User',
			component:User
		}
	]
})

// 前置路由守卫
router.beforeEach((to,from,next)=>{
	let path = to.path
	if(path.includes('/user') || path.includes('/details') ){
		if(!window.localStorage.getItem("user")){
			router.push('/my')
		}
	}
	next()
})

//  把router暴露出去
export default router